#include "CollisionEngine.h"

namespace collisions
{
	CollisionEngine::CollisionEngine()
	{

	};
	void CollisionEngine::checkCollisions(std::vector<sprites::Sprite*> _sprites)
	{
		for(unsigned int i = 0; i < _sprites.size(); i++)
		{
			if(_sprites[i]->isDead())//don't collide with dead objects
				continue;
			for(unsigned int j = i+1; j < _sprites.size(); j++)
			{
				if(_sprites[j]->isDead())//don't collide with dead objects
					continue;

				//collision checking logic
				BindingSphere sphere1 = _sprites[i]->getBindingSphere();
				BindingSphere sphere2 = _sprites[j]->getBindingSphere();
				bool collision = sphere1.collidesWith(sphere2);//checks if objects are near eachother

				if(collision)
				{
					vector<BindingSphere> spheres1 = _sprites[i]->getBindingShapes();
					vector<BindingSphere> spheres2 = _sprites[j]->getBindingShapes();
					//precise collision checking
					for(int x = 0; x < spheres1.size(); x++)
					{
						for(int y = 0; y < spheres2.size(); y++)
						{
							if(spheres1[x].collidesWith(spheres2[y]))
							{
								_sprites[i]->handleCollision(_sprites[j]->getMatterType(), sphere2);
								_sprites[j]->handleCollision(_sprites[i]->getMatterType(), sphere1);
							}
						}
					}
				}
			}
		}
	};
}